<template>
  <el-tabs
    @tab-click="tabClick"
    v-model="newActiveName1"
    type="border-card"
    class="ef-collect-w"
  >
    <el-tab-pane name="1" label="表位故障模拟" class="ll-content">
      <div v-if="activeName1 === '1'" class="ll-btn1">
        <el-form ref="twfFormRef" v-loading="faultLoading" :model="twfForm">
          <fieldset>
            <legend>表位故障:</legend>
            <div class="ll-bw">
              <fieldset>
                <legend>485线路故障:</legend>
                <div>
                  <el-checkbox-group
                    class="ll-bw-item1"
                    v-model="twfForm.ttype"
                  >
                    <el-checkbox label="A_DISCONNECT">485A断</el-checkbox>
                    <el-checkbox label="B_DISCONNECT">485B断</el-checkbox>
                    <el-checkbox label="A_B_CROSS">485AB反接</el-checkbox>
                    <el-checkbox label="A_B_BY_PASS">485AB短路</el-checkbox>
                  </el-checkbox-group>
                </div>
              </fieldset>
              <fieldset>
                <legend>电压线路故障:</legend>
                <div class="ll-bw-item2">
                  <div>
                    <el-checkbox-group class="item" v-model="twfForm.vlf">
                      <el-checkbox label="U_A_DISCONNECT"
                        >A相电压断路</el-checkbox
                      >
                      <el-checkbox label="U_B_DISCONNECT"
                        >B相电压断路</el-checkbox
                      >
                      <el-checkbox label="U_C_DISCONNECT"
                        >C相电压断路</el-checkbox
                      >
                      <el-checkbox label="U_N_DISCONNECT"
                        >N相电压断路</el-checkbox
                      >
                    </el-checkbox-group>
                  </div>
                  <div v-if="props.currentData.type != 6">
                    <fieldset>
                      <legend>电压相序:</legend>
                      <div>
                        <el-radio-group class="item" v-model="twfForm.vps">
                          <el-radio label="U_ABC">ABC</el-radio>
                          <el-radio label="U_ACB">ACB</el-radio>
                          <el-radio label="U_BAC">BAC</el-radio>
                          <el-radio label="U_BCA">BCA</el-radio>
                        </el-radio-group>
                      </div>
                    </fieldset>
                  </div>
                </div>
              </fieldset>
              <fieldset>
                <legend>电流线路故障:</legend>
                <div class="ll-bw-item2">
                  <div>
                    <el-checkbox-group class="item" v-model="twfForm.clf">
                      <el-checkbox label="IA_CROSS">A相电流反极</el-checkbox>
                      <el-checkbox label="IB_CROSS">B相电流反极</el-checkbox>
                      <el-checkbox label="IC_CROSS">C相电流反极</el-checkbox>
                    </el-checkbox-group>
                  </div>
                  <div>
                    <el-checkbox-group class="item" v-model="twfForm.clf">
                      <el-checkbox label="IA_PASS">IA短路</el-checkbox>
                      <el-checkbox label="IB_PASS">IB短路</el-checkbox>
                      <el-checkbox label="IC_PASS">IC短路</el-checkbox>
                    </el-checkbox-group>
                  </div>
                  <div class="item" v-if="props.currentData.type != 6">
                    <el-checkbox-group class="item" v-model="twfForm.clf">
                      <el-checkbox label="IA_OPEN">A相电流开路</el-checkbox>
                      <el-checkbox label="IB_OPEN">B相电流开路</el-checkbox>
                      <el-checkbox label="IC_OPEN">C相电流开路</el-checkbox>
                    </el-checkbox-group>
                  </div>
                  <div v-if="props.currentData.type != 6">
                    <fieldset>
                      <legend>电流线路故障:</legend>
                      <el-radio-group class="item" v-model="twfForm.cps">
                        <el-radio label="I_ABC">ABC</el-radio>
                        <el-radio label="I_CBA">CBA</el-radio>
                        <el-radio label="I_ACB">ACB</el-radio>
                        <el-radio label="I_CAB">CAB</el-radio>
                      </el-radio-group>
                    </fieldset>
                  </div>
                </div>
              </fieldset>
            </div>
          </fieldset>
          <fieldset class="ef-collect-w-1">
            <legend>表计故障:</legend>
            <div class="ll-bj" v-if="props.currentData.type != 6">
              <el-form class="ll-bj" ref="meterMalRef" :model="meterMalForm">
                <div>
                  <fieldset>
                    <legend>液晶故障设置:</legend>
                    <div>
                      <el-radio-group
                        v-model="meterMalForm.fault11"
                        class="item"
                      >
                        <el-radio label="1">正常</el-radio>
                        <el-radio label="2">终端液晶白屏</el-radio>
                        <el-radio label="3">终端液晶黑屏</el-radio>
                        <el-radio label="4">终端液晶花屏</el-radio>
                        <el-radio label="5">终端死机状态</el-radio>
                      </el-radio-group>
                    </div>
                  </fieldset>
                  <fieldset>
                    <legend style="width: 150px">RS485抄表故障设置:</legend>
                    <div>
                      <el-radio-group
                        v-model="meterMalForm.fault12"
                        class="item"
                        style="padding: 10px 10px"
                      >
                        <el-radio label="6">正常</el-radio>
                        <el-radio label="7">抄表485I故障</el-radio>
                        <el-radio label="8">抄表485II故障</el-radio>
                      </el-radio-group>
                    </div>
                  </fieldset>
                </div>
                <div>
                  <fieldset>
                    <legend>上行通讯参数:</legend>
                    <div class="item-border" style="margin: 19px 10px">
                      <el-radio-group
                        v-model="meterMalForm.fault3"
                        class="item"
                      >
                        <el-radio label="9">正常</el-radio>
                        <el-radio label="10">终端不上线,信号不好</el-radio>
                        <el-radio label="11">SIM卡故障</el-radio>
                        <el-radio label="12">模块通讯异常</el-radio>
                      </el-radio-group>
                    </div>
                    <div class="item-border" style="margin: 19px 10px">
                      <el-radio-group
                        v-model="meterMalForm.fault4"
                        class="item"
                      >
                        <el-radio label="13">正常</el-radio>
                        <el-radio label="14">以太网通讯故障</el-radio>
                        <el-radio label="15">以太网通讯不稳定故障</el-radio>
                      </el-radio-group>
                    </div>
                  </fieldset>
                </div>
                <div>
                  <fieldset>
                    <legend>电压异常故障:</legend>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault5"
                        class="item"
                      >
                        <el-radio label="16">正常</el-radio>
                        <el-radio label="17">A相失压</el-radio>
                        <el-radio label="18">A相失压为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault6"
                        class="item"
                      >
                        <el-radio label="19">正常</el-radio>
                        <el-radio label="20">B相失压</el-radio>
                        <el-radio label="21">B相失压为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault7"
                        class="item"
                      >
                        <el-radio label="22">正常</el-radio>
                        <el-radio label="23">C相失压</el-radio>
                        <el-radio label="24">C相失压为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                  </fieldset>
                </div>
                <div>
                  <fieldset>
                    <legend>电流异常故障:</legend>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault8"
                        class="item"
                      >
                        <el-radio label="25">正常</el-radio>
                        <el-radio label="26">A相失流</el-radio>
                        <el-radio label="27">A相失流为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault9"
                        class="item"
                      >
                        <el-radio label="28">正常</el-radio>
                        <el-radio label="29">B相失流</el-radio>
                        <el-radio label="30">B相失流为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                    <div class="item-border">
                      <el-radio-group
                        v-model="meterMalForm.fault10"
                        class="item"
                      >
                        <el-radio label="31">正常</el-radio>
                        <el-radio label="32">C相失流</el-radio>
                        <el-radio label="33">C相失流为真实值50%</el-radio>
                      </el-radio-group>
                    </div>
                  </fieldset>
                </div>
              </el-form>
            </div>
            <div v-else>
              <el-form class="ll-bj" ref="meterMalRef" :model="meterMalForm">
                <div class="ll-item1">
                  <div>
                    <el-checkbox-group
                      v-model="meterMalForm.fault1"
                      class="item"
                    >
                      <el-checkbox label="1">数据读取故障</el-checkbox>
                      <el-checkbox label="2">死机故障</el-checkbox>
                      <el-checkbox label="4">断相</el-checkbox>
                      <el-checkbox label="6">液晶显示</el-checkbox>
                      <el-checkbox label="5">电能表黑屏</el-checkbox>
                      <el-checkbox label="11">时间异常</el-checkbox>
                    </el-checkbox-group>
                  </div>
                  <div>
                    <el-checkbox-group
                      v-model="meterMalForm.fault2"
                      class="item"
                    >
                      <el-checkbox label="3">拉闸故障</el-checkbox>
                      <el-checkbox label="7">红外通信故障</el-checkbox>
                      <el-checkbox label="8">485回传故障</el-checkbox>
                      <el-checkbox label="9">失流故障</el-checkbox>
                      <el-checkbox label="10">失压故障</el-checkbox>
                    </el-checkbox-group>
                  </div>
                </div>
              </el-form>
            </div>
          </fieldset>
        </el-form>
      </div>
      <div v-if="activeName1 === '1'" class="ll-btn2">
        <el-button @click="clearSubmit()">恢复故障</el-button>
        <el-button @click="handleSubmit()">设置故障</el-button>
      </div>
    </el-tab-pane>
    <el-tab-pane v-if="currentData.type === 2" name="2" label="参数故障">
      <div v-if="activeName1 === '2'" class="lr-item">
        <el-tabs
          v-model="activeName"
          class="demo-tabs ef-collect-w-2"
          type="border-card"
        >
          <el-tab-pane label="通讯参数" name="first">
            <Communication
              :loading="faultLoading"
              :position="props.position"
              :currentTypes="currentData.type"
              @changeData="updateData"
              ref="communicationRef"
            ></Communication>
          </el-tab-pane>
          <el-tab-pane label="终端电能表/交流采样装置参数" name="second">
            <TerminalAmmeter
              :loading="faultLoading"
              :position="props.position"
              :currentTypes="currentData.type"
              @changeData="updateData01"
              ref="terminalAmmeterRef"
            ></TerminalAmmeter>
          </el-tab-pane>
        </el-tabs>
        <div class="opera-btn" v-if="activeName == 'first'">
          <el-button class="apply-button" @click="applyFault"
            >应用已读取的表地址</el-button
          >
          <el-button @click="clearFault">恢复故障</el-button>
          <el-button @click="setFault">设置故障</el-button>
        </div>
        <div class="opera-btn" v-else>
          <el-button class="apply-button" @click="applyFault1"
            >应用已读取的表地址</el-button
          >
          <el-button @click="clearFault1">恢复故障</el-button>
          <el-button @click="setFault1">设置故障</el-button>
        </div>
      </div>
    </el-tab-pane>
  </el-tabs>
</template>
<script setup>
import { ref, reactive, watch, nextTick, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useAppStore } from "@/stores";
import { storeToRefs } from "pinia";

const useApp = useAppStore();
let {
  faultLoading,
  currentPlanId,
  tableFaultParm,
  tableFaultSetting,
  isSuccess,
} = storeToRefs(useApp);
import {
  meterRelayFault,
  meterJzqFault,
  meterRelayFaultRecoveryAll,
  meterJzqFaultRecoveryAll,
  meterCommParmFalut,
  meterApplyMeterAddress,
  terminalSelectByPos,
  measuringPointParameterFaultSave,
  measuringPointParameterMeterParam,
  meterCldParmFalut,
  meterFzbFault,
  meterFzbSingleFault,
} from "@/api/source.js";

const props = defineProps({
  position: String,
  currentData: Object,
  activeName1: String,
});
const emit = defineEmits(["changeActiveName"]);
const tabPosition = ref("left");
const activeName = ref("first");
const newActiveName1 = ref(props.activeName1);
const loading = ref(false);

const currentData01 = ref();
const currentData1 = ref();
const radio = ref(1);
const lists = ref();
const twfFormRef = ref();
const meterMalRef = ref();
const communicationRef = ref();
const terminalAmmeterRef = ref();
let successNum = ref(0);
let errorNum = ref(0);
const twfForm = reactive({
  ttype: [],
  vlf: [],
  vps: "U_ABC",
  clf: [],
  cps: "I_ABC",
});

const meterMalForm = reactive({
  fault1: [],
  fault2: [],
  fault3: "9",
  fault4: "13",
  fault5: "16",
  fault6: "19",
  fault7: "22",
  fault8: "25",
  fault9: "28",
  fault10: "31",
  fault11: "1",
  fault12: "6",
});
const isFlag = ref(false);
onMounted(() => {
  init(props.position);
  watch(
    twfForm,
    (newValue, oldValue) => {
      tableFaultParm.value[props.position + "a"] = newValue;
      // console.log("mmmNewValue3", newValue);
    },
    { immediate: true },
    { deep: true }
  );
  watch(
    meterMalForm,
    (newValue, oldValue) => {
      tableFaultParm.value[props.position + "b1"] = newValue;
      // console.log("mmmNewValue4", newValue);
    },
    { immediate: true },
    { deep: true }
  );
  // console.log("collect", tableFaultSetting.value, props.position);
});
//初始化数据
const init = (parm) => {
  // console.log("collect", parm, tableFaultSetting.value, props.position);
  if (parm != undefined && parm != "undefined" && parm) {
    if (tableFaultSetting.value[parm + "a"]) {
      twfForm.ttype = tableFaultSetting.value[parm + "a"].ttype;
      twfForm.vlf = tableFaultSetting.value[parm + "a"].vlf;
      twfForm.vps = tableFaultSetting.value[parm + "a"].vps;
      twfForm.clf = tableFaultSetting.value[parm + "a"].clf;
      twfForm.cps = tableFaultSetting.value[parm + "a"].cps;
    }
    if (tableFaultSetting.value[parm + "b1"]) {
      meterMalForm.fault1 = tableFaultSetting.value[parm + "b1"].fault1;
      meterMalForm.fault2 = tableFaultSetting.value[parm + "b1"].fault2;
      meterMalForm.fault3 = tableFaultSetting.value[parm + "b1"].fault3;
      meterMalForm.fault4 = tableFaultSetting.value[parm + "b1"].fault4;
      meterMalForm.fault5 = tableFaultSetting.value[parm + "b1"].fault5;
      meterMalForm.fault6 = tableFaultSetting.value[parm + "b1"].fault6;
      meterMalForm.fault7 = tableFaultSetting.value[parm + "b1"].fault7;
      meterMalForm.fault8 = tableFaultSetting.value[parm + "b1"].fault8;
      meterMalForm.fault9 = tableFaultSetting.value[parm + "b1"].fault9;
      meterMalForm.fault10 = tableFaultSetting.value[parm + "b1"].fault10;
      meterMalForm.fault11 = tableFaultSetting.value[parm + "b1"].fault11;
      meterMalForm.fault12 = tableFaultSetting.value[parm + "b1"].fault12;
    }
  }
};
// 设置故障
const handleSubmit = async (obj = "") => {
  let pos = Number(props.position);
  isFlag.value = false;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
    isSuccess.value = true;
    isFlag.value = true;
    if (tableFaultParm.value[pos + "a"]) {
      twfForm.ttype = tableFaultParm.value[pos + "a"].ttype;
      twfForm.vlf = tableFaultParm.value[pos + "a"].vlf;
      twfForm.vps = tableFaultParm.value[pos + "a"].vps;
      twfForm.clf = tableFaultParm.value[pos + "a"].clf;
      twfForm.cps = tableFaultParm.value[pos + "a"].cps;
    }
    if (tableFaultParm.value[pos + "b1"]) {
      meterMalForm.fault1 = tableFaultParm.value[pos + "b1"].fault1;
      meterMalForm.fault2 = tableFaultParm.value[pos + "b1"].fault2;
      meterMalForm.fault3 = tableFaultParm.value[pos + "b1"].fault3;
      meterMalForm.fault4 = tableFaultParm.value[pos + "b1"].fault4;
      meterMalForm.fault5 = tableFaultParm.value[pos + "b1"].fault5;
      meterMalForm.fault6 = tableFaultParm.value[pos + "b1"].fault6;
      meterMalForm.fault7 = tableFaultParm.value[pos + "b1"].fault7;
      meterMalForm.fault8 = tableFaultParm.value[pos + "b1"].fault8;
      meterMalForm.fault9 = tableFaultParm.value[pos + "b1"].fault9;
      meterMalForm.fault10 = tableFaultParm.value[pos + "b1"].fault10;
      meterMalForm.fault11 = tableFaultParm.value[pos + "b1"].fault11;
    }
  }
  faultLoading.value = true;
  let faultCodes1 = "";
  let faultCodes2 = "";
  let result = Object.values(twfForm);
  let meterMalResult = Object.values(meterMalForm);
  for (let i = 0; i < result.length; i++) {
    let str = result[i];
    if (Array.isArray(result[i])) {
      str = result[i].join(",");
    }
    if (str) {
      if (faultCodes1) faultCodes1 += "," + str;
      else faultCodes1 = str;
    }
  }
  for (let i = 0; i < meterMalResult.length; i++) {
    if (meterMalResult[i]) {
      if (faultCodes2) faultCodes2 += "," + meterMalResult[i];
      else faultCodes2 = meterMalResult[i];
    }
  }
  let parm = { planId: currentPlanId.value, pos, faultCodes1 };
  let parm1 = { planId: currentPlanId.value, pos, faultCodes2 };
  // console.log(parm, parm1);
  // isSuccess.value = false;
  //故障设置 分步发送
  meterRelayFaultApi(parm, parm1, faultCodes2, obj); //设置表位故障
};
// 清除故障
const clearSubmit = async (obj) => {
  let pos = Number(props.position);
  let type = props.currentData.type;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
    type = Number(obj.type);
    isSuccess.value = true;
  }
  faultLoading.value = true;
  let parm = {
    pos,
    planId: currentPlanId.value,
  };
  //1台区；2台区下终端；3台区下总表；4台区下用户表；5转变用户；6专变下终端；7专变下用户表；8用户表
  if (type === 2 || type === 6) {
    meterRelayFaultRecoveryAllApi(parm, obj); //设置表位故障
  }
};
//表位接线故障恢复所有
const meterRelayFaultRecoveryAllApi = async (parm, obj) => {
  let pos = props.position;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
  }
  await meterRelayFaultRecoveryAll(parm)
    .then((res) => {
      meterJzqFaultRecoveryAllApi(parm); //设置表计故障
      // console.log(res);
      twfForm.ttype = [];
      twfForm.vlf = [];
      twfForm.vps = "U_ABC";
      twfForm.clf = [];
      twfForm.cps = "I_ABC";
      meterMalForm.fault1 = [];
      meterMalForm.fault2 = [];
      meterMalForm.fault3 = "9";
      meterMalForm.fault4 = "13";
      meterMalForm.fault5 = "16";
      meterMalForm.fault6 = "19";
      meterMalForm.fault7 = "22";
      meterMalForm.fault8 = "25";
      meterMalForm.fault9 = "28";
      meterMalForm.fault10 = "31";
      meterMalForm.fault11 = "1";
      meterMalForm.fault12 = "6";
      tableFaultSetting.value[pos + "b1"] = meterMalForm;
      tableFaultSetting.value[pos + "a"] = twfForm;
    })
    .catch((err) => {
      console.log(err);
      meterJzqFaultRecoveryAllApi(parm); //设置表计故障
    });
};
//集中器故障恢复所有
const meterJzqFaultRecoveryAllApi = async (parm) => {
  await meterJzqFaultRecoveryAll(parm)
    .then((res) => {
      // console.log(res);
      twfForm.ttype = [];
      twfForm.vlf = [];
      twfForm.vps = "U_ABC";
      twfForm.clf = [];
      twfForm.cps = "I_ABC";
      meterMalForm.fault1 = [];
      meterMalForm.fault2 = [];
      meterMalForm.fault3 = "9";
      meterMalForm.fault4 = "13";
      meterMalForm.fault5 = "16";
      meterMalForm.fault6 = "19";
      meterMalForm.fault7 = "22";
      meterMalForm.fault8 = "25";
      meterMalForm.fault9 = "28";
      meterMalForm.fault10 = "31";
      meterMalForm.fault11 = "1";
      meterMalForm.fault12 = "6";
      faultLoading.value = false;
      isSuccess.value = false;
    })
    .catch((err) => {
      console.log(err);
      faultLoading.value = false;
      isSuccess.value = false;
    });
};
//设置表位故障
const meterRelayFaultApi = async (parm, parm1, faultCodes2, obj) => {
  let pos = props.position;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
  }
  await meterRelayFault(parm)
    .then((res) => {
      // console.log(res);
      secondStep(parm1, faultCodes2, obj);
      successNum.value++;
      getMessage();
      tableFaultSetting.value[pos + "a"] = twfForm;
    })
    .catch((err) => {
      secondStep(parm1, faultCodes2, obj);
      errorNum.value++;
      getMessage();
    });
};
//设置表计故障
const meterJzqFaultApi = async (parm, obj) => {
  let pos = props.position;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
  }
  await meterJzqFault(parm)
    .then((res) => {
      // console.log(res);
      tableFaultSetting.value[pos + "b1"] = meterMalForm;
      successNum.value++;
      getMessage();
    })
    .catch((err) => {
      errorNum.value++;
      getMessage();
    });
};
//设置三相表
const meterFzbFaultApi = async (parm, obj) => {
  let pos = props.position;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
  }
  await meterFzbFault(parm)
    .then((res) => {
      // console.log(res);
      tableFaultSetting.value[pos + "b1"] = meterMalForm;
      successNum.value++;
      getMessage();
    })
    .catch((err) => {
      errorNum.value++;
      getMessage();
    });
};
//设置单相表
const meterFzbSingleFaultApi = async (parm, obj) => {
  let pos = props.position;
  if (obj != "undefined" && obj && obj != undefined) {
    pos = Number(obj.position);
  }
  await meterFzbSingleFault(parm)
    .then((res) => {
      // console.log(res);
      tableFaultSetting.value[pos + "b1"] = meterMalForm;
      successNum.value++;
      getMessage();
    })
    .catch((err) => {
      console.log(err);
      errorNum.value++;
      getMessage();
    });
};
const getMessage = () => {
  let num = successNum.value + Number(errorNum.value);
  if (num === 2) {
    faultLoading.value = false;
    successNum.value = 0;
    errorNum.value = 0;
    if (isFlag.value) {
      isSuccess.value = false;
    }
  }
};
const secondStep = (parm1, faultCodes2, obj) => {
  let arr = [3, 4, 7, 8];
  //1台区；2台区下终端；3台区下总表；4台区下用户表；5转变用户；6专变下终端；7专变下用户表；8用户表
  let type = props.currentData.type;
  let pos = props.position;
  let wiringType = props.currentData.wiringType;
  if (obj != "undefined" && obj && obj != undefined) {
    type = Number(obj.type);
    pos = Number(obj.position);
    wiringType = obj.wiringType;
  }
  if (type === 2 || type === 6) {
    meterJzqFaultApi(parm1, obj); //设置表计故障
  }
  if (arr.includes(type)) {
    let parm2 = {
      planId: currentPlanId.value,
      pos,
      faultCodes: faultCodes2,
    };
    if (wiringType === "单相") {
      meterFzbSingleFaultApi(parm2, obj); //设置单相表
    } else {
      meterFzbFaultApi(parm2, obj); //设置三相表
    }
  }
};
//更新通讯参数信息
const updateData = (parm) => {
  // currentData.value = parm;
  let arr = {};
  parm.forEach((item) => {
    arr[item.prop] = item.value;
    arr["terId"] = item.terId;
  });
  currentData01.value = arr;
  // console.log("updateData", parm, arr);
};
const updateData01 = (parm) => {
  // console.log("updateData01", parm);
  currentData1.value = parm;
};
//通讯参数-设置故障
const setFault = async () => {
  // console.log("aa", currentData01.value);
  // die;
  let parm = JSON.parse(JSON.stringify(currentData01.value));
  parm.state = 1;
  parm.pos = Number(props.position);
  parm.planId = currentPlanId.value;
  await meterCommParmFalut(parm)
    .then((res) => {
      // console.log(res);
      ElMessage({
        type: "success",
        message: "设置成功",
      });
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "设置失败",
      });
    });
};
//通讯参数-清除故障
const clearFault = async () => {
  let parm = JSON.parse(JSON.stringify(currentData01.value));
  parm.state = 0;
  parm.pos = Number(props.position);
  parm.planId = currentPlanId.value;
  await meterCommParmFalut(parm)
    .then((res) => {
      // console.log(res);
      communicationRef.value.terminalSelectByPosApi(); //重置数据列表
      ElMessage({
        type: "success",
        message: "清除成功",
      });
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "清除失败",
      });
    });
};
//通讯参数-应用已读取的表地址
const applyFault = async () => {
  let parm = {
    pos: props.position,
    planId: currentPlanId.value,
  };
  await meterApplyMeterAddress(parm)
    .then((res) => {
      // console.log(res);
      communicationRef.value.terminalSelectByPosApi(); //重置数据列表
      ElMessage({
        type: "success",
        message: "应用成功",
      });
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "应用失败",
      });
    });
};
//终端电能表/交流采样装置参数-设置故障
const setFault1 = async () => {
  let parm = currentData1.value;
  doSave1(parm);
  doSave2(1, 1);
};
const doSave1 = async (parm) => {
  //measuringPointParameterMeterParam
  await measuringPointParameterFaultSave(parm)
    .then((res) => {
      // console.log(res);
      ElMessage({
        type: "success",
        message: "设置成功",
      });
      faultLoading.value = false;
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "设置失败",
      });
      faultLoading.value = false;
    });
};
const doSave2 = async (state, type) => {
  let parm = {
    pos: Number(props.position),
    planId: currentPlanId.value,
    state,
  };
  await meterCldParmFalut(parm)
    .then((res) => {
      // console.log(res);
      ElMessage({
        type: "success",
        message: "设置成功",
      });
      if (type === 2) terminalAmmeterRef.value.terminalSelectByPosApi(); //重置数据列表
      faultLoading.value = false;
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "设置失败",
      });
      faultLoading.value = false;
    });
};
//终端电能表/交流采样装置参数-清除故障
const clearFault1 = async () => {
  doSave2(0, 2);
};
//终端电能表/交流采样装置参数-应用已读取的表地址
const applyFault1 = async () => {
  let parm = {
    pos: props.position,
    planId: currentPlanId.value,
  };
  await meterApplyMeterAddress(parm)
    .then((res) => {
      // console.log(res);
      ElMessage({
        type: "success",
        message: "应用成功",
      });
      terminalAmmeterRef.value.terminalSelectByPosApi(); //重置数据列表
    })
    .catch((err) => {
      console.log(err);
      ElMessage({
        type: "warning",
        message: "应用失败",
      });
    });
};
const tabClick = (obj) => {
  emit("changeActiveName", obj.props.name);
};
defineExpose({
  init,
  handleSubmit,
  clearSubmit,
});
</script>

<style lang="scss" scoped>
//功率源
.line-Loss {
  .ll-content {
    display: flex;
    .ll-bw {
      display: flex;
      justify-content: center;
      .ll-bw-item1 {
        display: flex;
        flex-direction: column;
        padding: 2px 10px;
      }
      .ll-bw-item2 {
        display: flex;
      }
    }
    .ll-bj {
      display: flex;
      justify-content: space-around;
      padding: 2px 5px;
      .ll-item1 {
        display: flex;
        justify-content: center;
      }
    }
    .el-checkbox {
      height: 24px;
    }
    .el-radio {
      height: 22px;
    }
    .item-border {
      border: 1px solid #ebeef5;
      margin: 2px 10px;
    }
    .item {
      display: flex;
      flex-direction: column;
      padding: 2px 2px;
    }
  }
  .ll-btn1 {
    flex-grow: 1;
  }
  .ll-btn2 {
    width: 100px;
    display: flex;
    flex-direction: column;
    align-items: end;
    justify-content: end;
    margin-bottom: 50px;
    .el-button {
      width: 90px;
      margin-top: 10px;
    }
  }
}
.opera-btn {
  margin-top: 10px;
  .el-button {
    cursor: pointer;
  }
}
</style>
<style lang="scss">
.lr-item {
  .el-tabs__content {
    min-height: 200px !important;
  }
}
.line-Loss {
  .ll-btn2 {
    width: 100px;
    display: flex;
    flex-direction: column;
    align-items: end;
    justify-content: end;
    margin-bottom: 10px !important;
    .el-button {
      width: 90px;
      margin-top: 10px;
    }
  }
}
</style>
